]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
popupMenu: Replace event handlers with click action
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 19 Apr 2023 12:11:09 +0000 (14:11 +0200)
committerMarge Bot <marge-bot@gnome.org>
Tue, 25 Apr 2023 09:01:16 +0000 (09:01 +0000)
Actions provide a higher-level API than event handlers, not unlike
GTK's event controllers (albeit less complete). Allowing them to
take care of the low-level bits where possible is generally a good
idea.

Menu items are a very straight-forward case, with a good amount
of code saving, so port them over.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2742>

js/ui/popupMenu.js

index c5fecc0ae8957b83be167a599cecfa780ad1ac9d..07be67ef36826275ddf0ee96f2afc7a77fb6db63 100644 (file)
@@ -97,6 +97,19 @@ var PopupBaseMenuItem = GObject.registerClass({
         this._activatable = params.reactive && params.activate;
         this._sensitive = true;
 
+        this._clickAction = new Clutter.ClickAction({
+            enabled: this._activatable,
+        });
+        this._clickAction.connect('clicked',
+            () => this.activate(Clutter.get_current_event()));
+        this._clickAction.connect('notify::pressed', () => {
+            if (this._clickAction.pressed)
+                this.add_style_pseudo_class('active');
+            else
+                this.remove_style_pseudo_class('active');
+        });
+        this.add_action(this._clickAction);
+
         if (!this._activatable)
             this.add_style_class_name('popup-inactive-menu-item');
 
@@ -124,44 +137,6 @@ var PopupBaseMenuItem = GObject.registerClass({
         this._parent = parent;
     }
 
-    vfunc_button_press_event() {
-        if (!this._activatable)
-            return Clutter.EVENT_PROPAGATE;
-
-        // This is the CSS active state
-        this.add_style_pseudo_class('active');
-        return Clutter.EVENT_PROPAGATE;
-    }
-
-    vfunc_button_release_event() {
-        if (!this._activatable)
-            return Clutter.EVENT_PROPAGATE;
-
-        this.remove_style_pseudo_class('active');
-
-        // Pointer left the item during the grab
-        if (!this.hover)
-            return Clutter.EVENT_PROPAGATE;
-
-        this.activate(Clutter.get_current_event());
-        return Clutter.EVENT_STOP;
-    }
-
-    vfunc_touch_event(touchEvent) {
-        if (!this._activatable)
-            return Clutter.EVENT_PROPAGATE;
-
-        if (touchEvent.type == Clutter.EventType.TOUCH_END) {
-            this.remove_style_pseudo_class('active');
-            this.activate(Clutter.get_current_event());
-            return Clutter.EVENT_STOP;
-        } else if (touchEvent.type == Clutter.EventType.TOUCH_BEGIN) {
-            // This is the CSS active state
-            this.add_style_pseudo_class('active');
-        }
-        return Clutter.EVENT_PROPAGATE;
-    }
-
     vfunc_key_press_event(keyEvent) {
         if (global.focus_manager.navigate_from_event(Clutter.get_current_event()))
             return Clutter.EVENT_STOP;
@@ -1267,25 +1242,7 @@ class PopupSubMenuMenuItem extends PopupBaseMenuItem {
     }
 
     activate(_event) {
-        this._setOpenState(true);
-    }
-
-    vfunc_button_release_event() {
-        // Since we override the parent, we need to manage what the parent does
-        // with the active style class
-        this.remove_style_pseudo_class('active');
         this._setOpenState(!this._getOpenState());
-        return Clutter.EVENT_PROPAGATE;
-    }
-
-    vfunc_touch_event(touchEvent) {
-        if (touchEvent.type == Clutter.EventType.TOUCH_END) {
-            // Since we override the parent, we need to manage what the parent does
-            // with the active style class
-            this.remove_style_pseudo_class('active');
-            this._setOpenState(!this._getOpenState());
-        }
-        return Clutter.EVENT_PROPAGATE;
     }
 });